/*
 * @Author: szx
 * @Date: 2022-03-17 13:45:40
 * @LastEditTime: 2022-03-17 13:56:39
 * @Description:
 * @FilePath: \leetcode\100-199\134\134_2.js
 */
/**
 * @param {number[]} gas
 * @param {number[]} cost
 * @return {number}
 */
var canCompleteCircuit = function (gas, cost) {
    let cnt = 0;
    let res = -1;
    let total = 0;
    for (let i = 0; i < gas.length; i++) {
        cnt += gas[i] - cost[i];
        total = gas[i] - cost[i];
        if (cnt < 0) {
            // 如果当前的剩余量小于0，那么说明肯定起点不是这里
            res = i + 1;
            cnt = 0;
        }
    }
    return total >= 0 ? res : -1;
};
